package com.uplift.common;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class Const {
	
	private final Logger logger = LogManager.getLogger(Const.class);
	
	/** HttpSession中用户信息的Key值 */
	public static String SESSION_USER = "session_user";
	
	/** 上传文件存储的目录 */
	public static String DIR_UPLOAD_BASE = "/home/upload/";
	
	/** 不受保护地址 */
	public static List<String> UNPROTECT_URL = new ArrayList<>();
	
	/** 百度地图ak */
	public static String MAP_BAIDU_AK = "ugC0aYGxoPcuS1wZ9nIoL5q7OFmUk2jy";
	
	/** 百度地图境外API */
	public static String MAP_BAIDU_API_PLACE_ABROAD = "http://api.map.baidu.com/place_abroad/v1/search";
	
	/** 发件人 */
	public static String MAIL_SENDER = "";
	
	@Value("${dir.upload.base}")
	public void setUploadDir(String dir) {
		DIR_UPLOAD_BASE = dir;
		try {
			File fileDir = new File(DIR_UPLOAD_BASE);
			if (!fileDir.exists()) {
				fileDir.mkdirs();
			}
		} catch (Exception e) {
			logger.error("【系统】创建目录失败：" + dir);
		}
		logger.info("【系统】上传文件存储目录：" + dir);
	}
	
	@Value("${url.unprotect}")
	public void setUnprotectUrl(String url) {
		String[] unprotectUrlArr = StringUtils.split(url, ",");
		if (unprotectUrlArr != null) {
			UNPROTECT_URL = Arrays.asList(unprotectUrlArr);
		}
		logger.info("【系统】加载不受保护地址：" + UNPROTECT_URL.size() + "个");
	}
	
	@Value("${map.baidu.ak}")
	public void setBaiduMapAK(String ak) {
		MAP_BAIDU_AK = ak;
		logger.info("【系统】加载百度地图AK完成");
	}
	
	@Value("${map.baidu.api.place.abroad}")
	public void setBaiduMapApiPlaceAbroad(String api) {
		MAP_BAIDU_API_PLACE_ABROAD = api;
		logger.info("【系统】加载百度地图境外API完成");
	}
	
	@Value("${mail.sender}")
	public void setMailSender(String mailSender) {
		MAIL_SENDER = mailSender;
		logger.info("【系统】系统邮件发件人: " + mailSender);
	}
}
